package edu.hrbeu.crc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.hrbeu.crc.constant.ErrorConstant;
import edu.hrbeu.crc.context.BaseContext;
import edu.hrbeu.crc.mapper.SavingPlanMapper;
import edu.hrbeu.crc.pojo.dto.SavingPlanDTO;
import edu.hrbeu.crc.pojo.entity.SavingPlan;
import edu.hrbeu.crc.pojo.query.SavingPlanQuery;
import edu.hrbeu.crc.pojo.result.PageResult;
import edu.hrbeu.crc.pojo.vo.SavingPlanPageVO;
import edu.hrbeu.crc.pojo.vo.SavingPlanVO;
import edu.hrbeu.crc.service.SavingPlanService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SavingPlanServiceImpl extends ServiceImpl<SavingPlanMapper, SavingPlan> implements SavingPlanService {

    /**
     * 添加计划
     * @param savingPlanDTO
     */
    @Override
    public String add(SavingPlanDTO savingPlanDTO) {
        // 如果当前金额>=目标金额
        if (savingPlanDTO.getCurrentAmount() != null && savingPlanDTO.getCurrentAmount() >= savingPlanDTO.getTargetAmount()) {
            return ErrorConstant.CURRENT_GE_TARGET;
        }
        SavingPlan plan = BeanUtil.copyProperties(savingPlanDTO, SavingPlan.class);

        plan.setUserId(BaseContext.getCurrentId());
        plan.setCreateTime(LocalDateTime.now());
        this.save(plan);
        return null;
    }

    /**
     * 分页查询
     * @param savingPlanQuery
     * @return
     */
    @Override
    public PageResult<SavingPlanPageVO> querySavingPlan(SavingPlanQuery savingPlanQuery) {
        if (savingPlanQuery.getPageNo() == null) {
            savingPlanQuery.setPageNo(1);
        }
        Page<SavingPlan> page = savingPlanQuery.toDefaultSortByCreateTimeDesc();

        // wrapper
        QueryWrapper<SavingPlan> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(SavingPlan::getUserId, BaseContext.getCurrentId())
                .like(savingPlanQuery.getName() != null, SavingPlan::getName, savingPlanQuery.getName())
                .apply("current_Amount < target_amount");

        Page<SavingPlan> p = this.page(page, wrapper);
        List<SavingPlan> records = p.getRecords();
        List<SavingPlanPageVO> vos = BeanUtil.copyToList(records, SavingPlanPageVO.class);

        return PageResult.<SavingPlanPageVO>builder()
                .total(p.getTotal())
                .page(p.getPages())
                .list(vos)
                .build();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public SavingPlanVO get(Integer id) {
        SavingPlan savingPlan = this.getById(id);

        return BeanUtil.copyProperties(savingPlan, SavingPlanVO.class);
    }

    /**
     * 修改计划
     * @param savingPlanDTO
     */
    @Override
    public void updateSavingPlan(SavingPlanDTO savingPlanDTO) {
        UpdateWrapper<SavingPlan> wrapper = new UpdateWrapper<>();

        wrapper.lambda()
                .set(savingPlanDTO.getName() != null, SavingPlan::getName, savingPlanDTO.getName())
                .set(savingPlanDTO.getCurrentAmount() != null, SavingPlan::getCurrentAmount, savingPlanDTO.getCurrentAmount())
                .set(savingPlanDTO.getRemark() != null, SavingPlan::getRemark, savingPlanDTO.getRemark())
                .eq(SavingPlan::getId, savingPlanDTO.getId());

        this.update(wrapper);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Integer> ids) {
        this.removeByIds(ids);
    }
}
